/*
 * Copyright 2014-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.restdocs.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.junit.runners.model.Statement;

import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.util.FileCopyUtils;

import static org.assertj.core.api.Assertions.fail;

/**
 * The {@code GeneratedSnippets} rule is used to capture the snippets generated by a test
 * and assert their existence and content.
 *
 * @author Andy Wilkinson
 * @author Andreas Evers
 */
public class GeneratedSnippets extends OperationTestRule {

	private final TemplateFormat templateFormat;

	private String operationName;

	private File outputDirectory;

	public GeneratedSnippets(TemplateFormat templateFormat) {
		this.templateFormat = templateFormat;
	}

	@Override
	public Statement apply(Statement base, File outputDirectory, String operationName) {
		this.outputDirectory = outputDirectory;
		this.operationName = operationName;
		return base;
	}

	public String curlRequest() {
		return snippet("curl-request");
	}

	public String httpieRequest() {
		return snippet("httpie-request");
	}

	public String requestHeaders() {
		return snippet("request-headers");
	}

	public String responseHeaders() {
		return snippet("response-headers");
	}

	public String httpRequest() {
		return snippet("http-request");
	}

	public String httpResponse() {
		return snippet("http-response");
	}

	public String links() {
		return snippet("links");
	}

	public String requestFields() {
		return snippet("request-fields");
	}

	public String requestParts() {
		return snippet("request-parts");
	}

	public String requestPartFields(String partName) {
		return snippet("request-part-" + partName + "-fields");
	}

	public String responseFields() {
		return snippet("response-fields");
	}

	public String pathParameters() {
		return snippet("path-parameters");
	}

	public String requestParameters() {
		return snippet("request-parameters");
	}

	public String snippet(String name) {
		File snippetFile = getSnippetFile(name);
		try {
			return FileCopyUtils.copyToString(new InputStreamReader(
					new FileInputStream(snippetFile), StandardCharsets.UTF_8));
		}
		catch (Exception ex) {
			fail("Failed to read '" + snippetFile + "'", ex);
			return null;
		}
	}

	private File getSnippetFile(String name) {
		if (this.outputDirectory == null) {
			fail("Output directory was null");
		}
		if (this.operationName == null) {
			fail("Operation name was null");
		}
		File snippetDir = new File(this.outputDirectory, this.operationName);
		return new File(snippetDir, name + "." + this.templateFormat.getFileExtension());
	}

}
